package com.fjut.kms.dao;

import com.fjut.kms.domain.UserLicenseReq;
import com.fjut.kms.tools.DBTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Empathy on 2017/7/30.
 */
public class UserLicenseReqJDBCImpl implements UserLicenseReqDao{
    private static final String SQL_INSERT="insert into userLicenseReq (deptname,username,softName,reqQuantity,reason,reqDate,status)values(?,?,?,?,?,?,?)";
    private static final String GETLICENSEREQ_BYUSERNAME="select * from userLicenseReq where username=?";
    public void addUserLicenseReq(UserLicenseReq ulr){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        try{
            pstmt=conn.prepareStatement(SQL_INSERT);
            pstmt.setString(1,ulr.getDeptName());
            pstmt.setString(2,ulr.getUserName());
            pstmt.setString(3,ulr.getSoftName());
            pstmt.setString(4,ulr.getReqQuantity());
            pstmt.setString(5,ulr.getReason());
            pstmt.setString(6,ulr.getReqDate());
            pstmt.setString(7,ulr.getStatus());
            pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
    }

    public List<UserLicenseReq> getUserLicenseReq(String userName){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        ResultSet rset=null;
        List<UserLicenseReq> ulrList=new ArrayList<UserLicenseReq>();
        try{
            pstmt=conn.prepareStatement(GETLICENSEREQ_BYUSERNAME);
            pstmt.setString(1,userName);
            rset=pstmt.executeQuery();
            while (rset.next()){
                UserLicenseReq ulr=new UserLicenseReq();
                ulr.setUserName(userName);
                ulr.setSoftName(rset.getString("softName"));
                ulr.setReqQuantity(rset.getString("reqQuantity"));
                ulr.setReason(rset.getString("reason"));
                ulr.setReqDate(rset.getString("reqDate"));
                ulr.setAssigenNum(rset.getString("assignNum"));
                ulr.setAssignDate(rset.getString("assignDate"));
                ulr.setStatus(rset.getString("status"));
                ulrList.add(ulr);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
        return ulrList.size()>0?ulrList:null;
    }
}
